package com.example.proxy.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class LogHandler implements InvocationHandler {

    Object target = null;

    public LogHandler(Object target) {
        super();
        this.target = target;
    }


    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开启日志..."); //执行核心业务之前执行的内容
        Object invoke = method.invoke(target, args);//执行目标对象方法，即核心业务
        System.out.println("关闭日志..."); //执行核心业务之后执行的内容
        return invoke;
    }

    public <T> T  getInstance() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
}
